package J5_17;

import java.util.*;

public class test {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
        }
        int res = 0;
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                res++;
                break;
            }
            if (arr[i + 1] > arr[i]) {
                res++;
                i++;
                while (i + 1 < arr.length && arr[i + 1] >= arr[i]) {
                    i++;
                }
            } else if (arr[i + 1] < arr[i]) {
                res++;
                i++;
                while (i + 1 < arr.length && arr[i + 1] <= arr[i]) {
                    i++;
                }
            } else {
                while (i + 1 < arr.length && arr[i + 1] == arr[i]) {
                    i++;
                }
            }
        }
        System.out.println(res);
    }

    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        while (n-- > 0) {
            int cur = in.nextInt();
            int[] arr = new int[]{cur, 1};
            int count1 = f(arr);
            int count2 = f2(arr);
            int res = count1 + count2;
            System.out.println(res);
        }
    }

    private static int f2(int[] arr) {
        arr[1] /= 2;
        int res = 0;
        while (arr[0] > 0 ){
            if (arr[0] >= arr[1]) {
                arr[0] -= arr[1];
                res++;
            } else {
                arr[1] /= 2;
            }
        }
        return res;
    }

    private static int f(int[] arr) {
        int res = 0;
        while (arr[0] >= arr[1]) {
            arr[0] -= arr[1];
            arr[1] *= 2;
            res++;
        }
        return res;
    }

    public static int LIS (int[] a) {
        if (a.length == 0) {
            return 0;
        }
        List<Integer> list = new ArrayList<>();
        list.add(a[0]);
        for (int i = 1; i < a.length; i++) {
            if (a[i] > list.get(list.size() - 1)) {
                list.add(a[i]);
            } else if (a[i] < list.get(list.size() - 1)) {
                int index = getIndex(list, a[i]);
                list.set(index, a[i]);
            }
        }
        return list.size();
    }

    private static int getIndex(List<Integer> list, int i) {
        int l = 0, r = list.size() - 1;
        while (l < r) {
            int mid = (l + r) / 2;
            if (list.get(mid) < i) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        return l;
    }

    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
    int n = in.nextInt();
        while (n-- > 0) {
        int a = in.nextInt(), b = in.nextInt();
        long t = a * b;
        if (a == 1|| b == 1) {
            System.out.println(isPri(t));
        } else {
            System.out.println("NO");
        }
    }
}
    private static String isPri(long a) {
        for (int i = 2; i <= Math.sqrt(a); i++) {
            if (a % i == 0) return "NO";
        }
        return "YES";
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int cur = in.nextInt();
            map.put(cur, map.getOrDefault(cur, 0)+1);
        }

        int[][] arr = new int[map.size()][2];
        int pos = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            arr[pos][0] = entry.getKey();
            arr[pos][1] = entry.getValue();
            pos++;
        }
        if (map.size() == 1) {
            System.out.println(arr[0][1]);
            return;
        }
        Arrays.sort(arr, (a, b) -> a[0]-b[0]);
        int res = arr[0][1] , max = res, p = 0;
        for (int i = 0; i < arr.length; i++) {
            while (p + 1 < arr.length && arr[p + 1][0] - arr[i][0] <= k) {
                p++;
                res += arr[p][1];
                max = Math.max(max, res);
            }
            if (p == arr.length - 1){
                break;
            }
            res -= arr[i][1];
        }
        System.out.println(max);
    }
}
